import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:yqmssa_app/config/app_config.dart';
import 'package:yqmssa_app/request/http_path.dart';
import 'package:yqmssa_app/request/my_request.dart';
import 'package:yqmssa_app/router/page_router.dart';
import 'package:yqmssa_app/utils/shared_utils.dart';
import 'package:yqmssa_app/views/index/screen/home_manager.dart';
import 'package:yqmssa_app/widget/default_dialog_widget.dart';

class MineScreen extends StatelessWidget {
  const MineScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          Image.asset(AppConfig.imagePath("mine_bg.png")),
          ListView(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            children: [
              const SizedBox(height: 96),
              Row(
                children: [
                  Image.asset(
                    AppConfig.imagePath("self_avatar.png"),
                    width: 64,
                    height: 64,
                  ),
                  const SizedBox(width: 12),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        sharedUtils.userPhone.replaceRange(3, 7, "****"),
                        style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w500, color: Color(0XFF333333)),
                      ),
                      const SizedBox(height: 4),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset(AppConfig.imagePath("safe_icon.png"), width: 14),
                          const SizedBox(width: 6),
                          const Text(
                            "请妥善保管好您的个人信息",
                            style: TextStyle(fontSize: 12, color: Color(0XFF666666)),
                          ),
                        ],
                      ),
                    ],
                  )
                ],
              ),
              const SizedBox(height: 40),
              InkWell(
                  onTap: () {
                    final a = Get.find<HomeManager>().videosData.first;
                    Get.toNamed(PageRouter.landing, parameters: {"id": "${a.id}"});
                  },
                  child: Image.asset(AppConfig.imagePath("mine_banner.png"))),
              const SizedBox(height: 40),
              const Text("我的功能", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
              const SizedBox(height: 16),
              GridView(
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4, childAspectRatio: 1),
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: EdgeInsets.zero,
                children: [
                  _menuItem(
                      title: "关于我们",
                      icon: "about_icon.png",
                      onTap: () {
                        Get.toNamed(PageRouter.about);
                      }),
                  _menuItem(
                      title: "意见反馈",
                      icon: "feedback_icon.png",
                      onTap: () {
                        Get.toNamed(PageRouter.feedback);
                      }),
                  _menuItem(
                      title: "隐私政策",
                      icon: "privacy_icon.png",
                      onTap: () {
                        Get.toNamed(PageRouter.agreement, parameters: {"title": "隐私政策", "url": AppConfig.xieYIData.privacyAgreementUrl});
                      }),
                  _menuItem(
                      title: "用户协议",
                      icon: "user_icon.png",
                      onTap: () {
                        Get.toNamed(PageRouter.agreement, parameters: {"title": "用户协议", "url": AppConfig.xieYIData.userAgreementUrl});
                      }),
                  // _menuItem(
                  //     title: "系统设置",
                  //     icon: "setting_icon.png",
                  //     onTap: () {
                  //       Get.toNamed(PageRouter.about);
                  //     }),
                  _menuItem(
                    title: "退出登录",
                    icon: "logout_icon.png",
                    onTap: () async {
                      final res = await Get.dialog(const DefaultDialogWidget(
                        title: "退出登录提示",
                        content: "退出登录,您的提交报名记录和会话聊天记录将会被删除，确定要退出登录吗？",
                        confirmText: "退出登录",
                      ));
                      if (res != null && res) {
                        sharedUtils.logOut();
                        Get.offAllNamed(PageRouter.login);
                      }
                    },
                  ),
                  _menuItem(
                      title: "注销账户",
                      icon: "power_icon.png",
                      onTap: () async {
                        final res = await Get.dialog(const DefaultDialogWidget(
                          title: "账号注销提示",
                          content: "注销账号后，您的所有数据将被清除，是否继续注销？",
                          confirmText: "立即注销",
                        ));
                        if (res != null && res) {
                          SmartDialog.showLoading(msg: "注销中...");
                          MyRequest().dioReq(HttpPath.dispose).then((value) {
                            sharedUtils.logOut();
                            SmartDialog.dismiss();
                            Get.offAllNamed(PageRouter.login);
                          }).catchError((err) {
                            SmartDialog.dismiss();
                          });
                        }
                      }),
                ],
              )
            ],
          ),
        ],
      ),
    );
  }

  Widget _menuItem({required String title, required String icon, VoidCallback? onTap}) {
    return InkWell(
        onTap: onTap,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(AppConfig.imagePath(icon), width: 24, height: 24),
            const SizedBox(height: 4),
            Text(title, style: const TextStyle(color: Color(0XFF555555))),
          ],
        ));
  }
}
